
package com.applabs.dnd.provider;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class DNDMeProfileProvider extends ContentProvider {

    private SQLiteDatabase sqlDB;

    private DNDMeDbaseHelper mDbHelper;

    private static final String TABLE_NAME = "DndProfiles";

    private static final String TAG = "DNDMeProfileProvider";

    public static final String AUTHORITY = "com.applabs.dnd.provider";

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/dndprofile");

    private static final UriMatcher sUriMatcher;

    private static final int DNDPROFILE_TYPE_LIST = 1;

    private static final int DNDPROFILE_TYPE_ONE = 2;

    public static final String CONTENT_TYPE_DNDPROFILE_LIST = "com.applabs.dnd.provider.dir/com.applabs.dnd.provider.dndprofile";

    public static final String CONTENT_TYPE_DNDPROFILE_ONE = "com.applabs.dnd.provider.item/com.applabs.dnd.provider.dndprofile";

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(DNDMeProfileProvider.AUTHORITY, "dndprofiles", DNDPROFILE_TYPE_LIST);
        sUriMatcher.addURI(DNDMeProfileProvider.AUTHORITY, "dndprofiles/#", DNDPROFILE_TYPE_ONE);
    }

    private static final HashMap<String, String> sColsProjectionMap;
    static {
        sColsProjectionMap = new HashMap<String, String>();
        sColsProjectionMap.put(DNDMeColumns.DPROFILE_ID, DNDMeColumns.DPROFILE_ID);
        sColsProjectionMap.put(DNDMeColumns.DPROFILE_NAME, DNDMeColumns.DPROFILE_NAME);
        sColsProjectionMap
                .put(DNDMeColumns.DPROFILE_ISWHITELIST, DNDMeColumns.DPROFILE_ISWHITELIST);
        sColsProjectionMap.put(DNDMeColumns.DPROFILE_CALLRULETYPE,
                DNDMeColumns.DPROFILE_CALLRULETYPE);
        sColsProjectionMap
                .put(DNDMeColumns.DPROFILE_CALLREJTYPE, DNDMeColumns.DPROFILE_CALLREJTYPE);
        sColsProjectionMap.put(DNDMeColumns.DPROFILE_NUMCALLSTODISABLE,
                DNDMeColumns.DPROFILE_NUMCALLSTODISABLE);
        sColsProjectionMap.put(DNDMeColumns.DPROFILE_REJMSG, DNDMeColumns.DPROFILE_REJMSG);
        sColsProjectionMap.put(DNDMeColumns.DPROFILE_ENABLE_CALENDER,
                DNDMeColumns.DPROFILE_ENABLE_CALENDER);
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        int count = 0;
        switch (sUriMatcher.match(uri)) {
            case DNDPROFILE_TYPE_LIST:
                count = db.delete(TABLE_NAME, where, whereArgs);
                break;

            case DNDPROFILE_TYPE_ONE:
                String rowId = uri.getPathSegments().get(1);
                count = db.delete(TABLE_NAME, DNDMeColumns.DPROFILE_ID + " = " + rowId
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ")" : ""), whereArgs);
                break;

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        Log.d(TAG, "delete success : " + count);
        return count;
    }

    @Override
    public String getType(Uri uri) {
        Log.d(TAG, "getType entry  ");
        switch (sUriMatcher.match(uri)) {
            case DNDPROFILE_TYPE_LIST:
                return CONTENT_TYPE_DNDPROFILE_LIST;

            case DNDPROFILE_TYPE_ONE:
                return CONTENT_TYPE_DNDPROFILE_ONE;

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentvalues) {
        // get database to insert records
        sqlDB = mDbHelper.getWritableDatabase();
        // insert record in user table and get the row number of recently
        // inserted record
        long rowId = sqlDB.insert(TABLE_NAME, "", contentvalues);
        if (rowId > 0) {
            Log.d(TAG, "insert successfull");
            Uri rowUri = ContentUris.withAppendedId(DNDMeProfileProvider.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(rowUri, null);
            return rowUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public boolean onCreate() {
        Log.d(TAG, "onCreate successfull");
        mDbHelper = new DNDMeDbaseHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String arg4) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        switch (sUriMatcher.match(uri)) {
            case DNDPROFILE_TYPE_LIST:
                builder.setTables(DNDMeProfileProvider.TABLE_NAME);
                builder.setProjectionMap(sColsProjectionMap);
                break;

            case DNDPROFILE_TYPE_ONE:
                builder.setTables(DNDMeProfileProvider.TABLE_NAME);
                builder.setProjectionMap(sColsProjectionMap);
                builder.appendWhere(DNDMeColumns.DPROFILE_ID + " = " + uri.getPathSegments().get(1));
                break;

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        SQLiteDatabase db = mDbHelper.getReadableDatabase();
        Cursor queryCursor = builder.query(db, projection, selection, selectionArgs, null, null,
                null);
        queryCursor.setNotificationUri(getContext().getContentResolver(), uri);

        return queryCursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        int count = 0;
        switch (sUriMatcher.match(uri)) {
            case DNDPROFILE_TYPE_LIST:
                count = db.update(TABLE_NAME, values, where, whereArgs);
                break;

            case DNDPROFILE_TYPE_ONE:
                String rowId = uri.getPathSegments().get(1);
                count = db.update(TABLE_NAME, values, DNDMeColumns.DPROFILE_ID + " = " + rowId
                        + (!TextUtils.isEmpty(where) ? " AND (" + ")" : ""), whereArgs);

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

}
